Passed
Pull Request — master (#2)
by Khalil
01:10
created

exports.client   C

Complexity

Conditions 10
Paths 12

Size

Total Lines 65
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 0 Features 1
Metric Value
cc 10
eloc 37
c 8
b 0
f 1
nc 12
dl 0
loc 65
rs 5.9999
nop 1

2 Functions

Rating   Name   Duplication   Size   Complexity  
A 0 4 3
A 0 8 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like exports.client often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/**
2
 * @author Jinzulen
3
 * @license Apache 2.0
4
 * 
5
 * TenorJS - Lightweight NodeJS wrapper around the Tenor.com API.
6
 */
7
const FS     = require("fs"),
8
      Colors = require("colors");
9
10
exports.client = function (Credentials)
11
{
12
      const Filters      = ["off", "low", "medium", "high"],
13
            MediaFilters = ["basic", "minimal"];
14
15
      /**
16
       * Credentials checks.
17
       */
18
      if (!Credentials.Key || !Credentials.Locale || !Credentials.Filter)
19
      {
20
            throw new Error ("Client configuration is not complete; please ensure all configuration parameters are satisfied (Key, Locale, Filter).");
21
      }
22
      
23
      if (!Filters.includes(Credentials.Filter.toLowerCase()))
24
      {
25
            throw new Error ("Content filter level has to be one of these options: off, low, medium, high.");
26
      }
27
28
      if (Credentials.MediaFilter)
29
      {
30
            if (!MediaFilters.includes(Credentials.MediaFilter.toLowerCase()))
31
            {
32
                  throw new Error ("Media content filter has to be one of these options: basic, minimal.");
33
            }
34
      } else if (!Credentials.MediaFilter) {
35
            Credentials.MediaFilter = "minimal"; 
36
      }
37
38
      if (!Credentials.DateFormat) Credentials.DateFormat = "D/MM/YYYY - H:mm:ss A";
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
39
40
      Credentials.Gate   = "https://api.tenor.com/v1";
41
      Credentials.Filter = Credentials.Filter.toLowerCase();
42
43
      /**
44
       * Should probably move this elsewhere in the future, not a good
45
       * idea having this much code and heavy-lifting in one place.
46
       */
47
      let Creds = JSON.stringify(Credentials);
48
      
49
      function writeConfig()
50
      {
51
            FS.writeFileSync("tenor_config.json", Creds, function (Error) {
52
                  if (Error) throw Error;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
53
                  console.log(Colors.bold.green(`# [TenorJS] Changes have been made to the configuration file. Process should be restarted.`));
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
54
                  process.exit(1);
0 ignored issues
show
Compatibility Debugging Code Best Practice introduced by
Use of process.exit() is discouraged as it will potentially stop the complete node.js application. Consider quitting gracefully instead by throwing an Error.
Loading history...
55
            });
56
      }
57
      
58
      try
59
      {
60
            if (!FS.existsSync("tenor_config.json"))
61
            {
62
                  writeConfig();
63
            } else {
64
                  FS.readFile("tenor_config.json", "utf8", function (Error, Data) {
65
                        if (Error) throw Error;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
66
                        if (Data !== Creds) writeConfig();
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
67
                  });
68
            }
69
      } catch (E) {
70
            throw E;
71
      }
72
73
      return require("./src")(Credentials);
74
};